package io.geekidea.boot.shop.service.base.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.geekidea.boot.framework.exception.BusinessException;
import io.geekidea.boot.framework.page.OrderByItem;
import io.geekidea.boot.framework.page.OrderMapping;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.shop.dto.InviteDto;
import io.geekidea.boot.shop.entity.Invite;
import io.geekidea.boot.shop.mapper.InviteMapper;
import io.geekidea.boot.shop.query.app.AppInviteQuery;
import io.geekidea.boot.shop.query.web.InviteQuery;
import io.geekidea.boot.shop.service.base.api.InviteService;
import io.geekidea.boot.shop.vo.app.AppInviteVo;
import io.geekidea.boot.shop.vo.web.InviteVo;
import io.geekidea.boot.util.PagingUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  服务实现类
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@Service
public class InviteServiceImpl extends ServiceImpl<InviteMapper, Invite> implements InviteService {

    @Autowired
    private InviteMapper inviteMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addInvite(InviteDto dto) {
        Invite invite = new Invite();
        BeanUtils.copyProperties(dto, invite);
        return save(invite);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateInvite(InviteDto dto) {
        String id = dto.getId();
        Invite invite = getById(id);
        if (invite == null) {
            throw new BusinessException("不存在");
        }
        BeanUtils.copyProperties(dto, invite);
        return updateById(invite);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteInvite(String id) {
        return removeById(id);
    }

    @Override
    public InviteVo getInviteById(String id) {
        return inviteMapper.getInviteById(id);
    }

    @Override
    public Paging<InviteVo> getInvitePage(InviteQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<InviteVo> list = inviteMapper.getInvitePage(query);
        Paging<InviteVo> paging = new Paging<>(list);
        return paging;
    }

    @Override
    public AppInviteVo getAppInviteById(String id) {
        return inviteMapper.getAppInviteById(id);
    }

    @Override
    public Paging<AppInviteVo> getAppInvitePage(AppInviteQuery query) {
        OrderMapping orderMapping = new OrderMapping();
        orderMapping.put("createTime", "create_time");
        PagingUtil.handlePage(query, orderMapping, OrderByItem.desc("id"));
        List<AppInviteVo> list = inviteMapper.getAppInvitePage(query);
        Paging<AppInviteVo> paging = new Paging<>(list);
        return paging;
    }

}
